package three;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

public class StreamingParser {
    public static void main(String[] args) throws IOException {

        //生成的json写入文件
//        generateJsonToFile();

//        生成的json写入字符串
//        generateJsonToString();

        //按照Token的方式解析json，有点类似sax的解析方式，将
        parserJsonByToken();

    }

    private static void parserJsonByToken() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory f = mapper.getJsonFactory();
        JsonParser p = f.createParser(new File("test.json"));

        //JsonToken.START_OBJECT
        JsonToken t = p.nextToken();

        //FIELD_NAME
        t = p.nextToken();
        if (t == JsonToken.FIELD_NAME) {
            System.out.println(p.getCurrentName());
        }

        t = p.nextToken();
        if (t == JsonToken.VALUE_STRING) {
            //get value
            System.out.println(p.getValueAsString());

            //get value
            String msg = p.getText();
            System.out.println(msg
            );
        }
        p.close();


    }

    /**
     * [
     * {
     * "name": "wpp",
     * "age": 1
     * },
     * {
     * "name": "qpp",
     * "age": 2
     * },
     * {
     * "name": "spp",
     * "age": 3
     * }
     * ]
     *
     * @throws IOException
     */
    private static void generateJsonToString() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory f = mapper.getJsonFactory();
//        Writer writer = new PrintWriter(System.out);
        Writer writer = new StringWriter();

        JsonGenerator g = f.createGenerator(writer);

        g.writeStartArray();

        g.writeStartObject();
        g.writeStringField("name", "wpp");
        g.writeNumberField("age", 1);
        g.writeEndObject();

        g.writeStartObject();
        g.writeStringField("name", "qpp");
        g.writeNumberField("age", 2);
        g.writeEndObject();


        g.writeStartObject();
        g.writeStringField("name", "spp");
        g.writeNumberField("age", 3);
        g.writeEndObject();

        g.writeEndArray();

        g.flush();
        g.close();

        String s = writer.toString();
        System.out.println(s);

    }

    private static void generateJsonToFile() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory f = mapper.getJsonFactory();
        JsonGenerator g = f.createGenerator(new File("test.json"), JsonEncoding.UTF8);
        g.writeStartObject();
        g.writeStringField("message", "hello jackson");
        g.writeEndObject();
        g.close();

    }
}
